package com.foodorder.repository;

import com.foodorder.entity.Store;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺数据访问接口
 */
@Repository
public interface StoreRepository extends JpaRepository<Store, Long>, JpaSpecificationExecutor<Store> {
    
    /**
     * 根据店铺状态查找店铺
     */
    Page<Store> findByStatus(Store.StoreStatus status, Pageable pageable);
    
    /**
     * 根据店铺名称模糊搜索
     */
    @Query("SELECT s FROM Store s WHERE s.name LIKE %:keyword% AND s.status = :status")
    Page<Store> findByNameContainingAndStatus(@Param("keyword") String keyword, @Param("status") Store.StoreStatus status, Pageable pageable);
    
    /**
     * 查找营业中的店铺
     */
    List<Store> findByStatus(Store.StoreStatus status);
    
    /**
     * 根据地理位置查找附近的店铺
     */
    @Query("SELECT s FROM Store s WHERE s.status = com.foodorder.entity.Store$StoreStatus.OPEN " +
           "AND (6371 * acos(cos(radians(:latitude)) * cos(radians(s.latitude)) * " +
           "cos(radians(s.longitude) - radians(:longitude)) + sin(radians(:latitude)) * " +
           "sin(radians(s.latitude)))) <= :radius ORDER BY " +
           "(6371 * acos(cos(radians(:latitude)) * cos(radians(s.latitude)) * " +
           "cos(radians(s.longitude) - radians(:longitude)) + sin(radians(:latitude)) * " +
           "sin(radians(s.latitude))))")
    List<Store> findNearbyStores(@Param("latitude") Double latitude, 
                                @Param("longitude") Double longitude, 
                                @Param("radius") Double radius);
    
    /**
     * 查找热门店铺（按订单数量排序）
     */
    @Query("SELECT s FROM Store s WHERE s.status = com.foodorder.entity.Store$StoreStatus.OPEN ORDER BY s.totalSales DESC")
    List<Store> findHotStores(Pageable pageable);
    
    /**
     * 统计营业中的店铺数量
     */
    long countByStatus(Store.StoreStatus status);
    
    /**
     * 根据业务状态统计店铺数量
     */
    default long countByBusinessStatus(Store.BusinessStatus businessStatus) {
        switch (businessStatus) {
            case OPEN:
                return countByStatus(Store.StoreStatus.OPEN);
            case CLOSED:
                return countByStatus(Store.StoreStatus.CLOSED);
            case SUSPENDED:
            case PAUSED:
                return countByStatus(Store.StoreStatus.SUSPENDED);
            default:
                return 0;
        }
    }
    
    /**
     * 根据业务状态查找店铺
     */
    default List<Store> findByBusinessStatus(Store.BusinessStatus businessStatus) {
        switch (businessStatus) {
            case OPEN:
                return findByStatus(Store.StoreStatus.OPEN);
            case CLOSED:
                return findByStatus(Store.StoreStatus.CLOSED);
            case SUSPENDED:
            case PAUSED:
                return findByStatus(Store.StoreStatus.SUSPENDED);
            default:
                return new ArrayList<>();
        }
    }
    
    /**
     * 按销量排序查找店铺
     */
    @Query("SELECT s FROM Store s WHERE s.status = com.foodorder.entity.Store$StoreStatus.OPEN " +
           "ORDER BY s.monthlySales DESC")
    Page<Store> findStoresByMonthlySales(Pageable pageable);
    
    /**
     * 按评分排序查找店铺
     */
    @Query("SELECT s FROM Store s WHERE s.status = com.foodorder.entity.Store$StoreStatus.OPEN " +
           "ORDER BY s.rating DESC")
    Page<Store> findStoresByRating(Pageable pageable);
    
    /**
     * 搜索店铺（按名称）
     */
    @Query("SELECT s FROM Store s WHERE s.status = com.foodorder.entity.Store$StoreStatus.OPEN AND " +
           "(s.name LIKE %:keyword% OR s.description LIKE %:keyword%)")
    Page<Store> searchStores(@Param("keyword") String keyword, Pageable pageable);
    

}